﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace objass
{
    public partial class SearchWindow : UserControl
    {
        private string specialText = "";
        public SearchWindow()
        {
            InitializeComponent();

            // 添加控件尺寸变化事件处理程序
            this.SizeChanged += SearchWindow_SizeChanged;

            // 初始化时隐藏 SearchSpecialButton
            SearchSpecialButton.Visibility = Visibility.Collapsed;
        }

        private void SearchWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // 输出 SearchWindow 的实际尺寸
            System.Diagnostics.Debug.WriteLine($"SearchWindow ActualWidth: {this.ActualWidth}, ActualHeight: {this.ActualHeight}");
        }

        private void FindText(string text)
        {
            // 清空 ListBox
            ListBox1.ItemsSource = null;
            ListBox2.ItemsSource = null;
            ListBox3.ItemsSource = null;
            ListBox4.ItemsSource = null;

            // 在这里添加查找逻辑
            var app = (App)Application.Current;
            var result = app.Search(text);

            // 将搜索结果绑定到 ListBox
            if (result != null && result.Count >= 4)
            {
                ListBox1.ItemsSource = result[0];
                ListBox2.ItemsSource = result[1];
                ListBox3.ItemsSource = result[2];
                ListBox4.ItemsSource = result[3];

                Label1.Content = $"一级检索:{result[0].Count}条";
                Label2.Content = $"二级检索:{result[1].Count}条";
                Label3.Content = $"三级检索:{result[2].Count}条";
                Label4.Content = $"四级检索:{result[3].Count}条";
            }
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            // 输出 SearchWindow 的实际尺寸
            //System.Diagnostics.Debug.WriteLine($"SearchWindow ActualWidth: {this.ActualWidth}, ActualHeight: {this.ActualHeight}");

            NodeInput nodeInputDialog = new NodeInput();
            if (nodeInputDialog.ShowDialog() == true)
            {
                SearchLabel.Content = nodeInputDialog.SelectedWord;
                FindText(SearchLabel.Content.ToString());
            }
        }

        private void SearchSpecialButton_Click(object sender, RoutedEventArgs e)
        {
            // 在这里添加特殊查找逻辑
            FindText(specialText);
            SearchSpecialButton.Visibility = Visibility.Collapsed;

            SearchLabel.Content = specialText;
            specialText = "";
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is ListBox listBox && listBox.SelectedItem is string selectedItem)
            {
                // 更新 InfoTextBox 的内容
                specialText = selectedItem.Split(':')[0];
                if (specialText.EndsWith("*"))
                {
                    specialText = specialText.Substring(0, specialText.Length - 1);
                }
                InfoTextBox.Text = selectedItem;
                var app = (App)Application.Current;
                InfoTextBox.Text += $"\n{app.GetNodeContent(specialText)}";
                SearchSpecialButton.Visibility = Visibility.Visible;
                SearchSpecialButton.Content = $"查找'{specialText}'";
            }
        }

    }
}
